/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : home.js                                 
*  Description  : 클럽개별메인 
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-06-20  이윤노    Created.  
*  2012-06-00  이윤노    Complete.  
*******************************************************************************/

jQuery(function($){
	
	function clsNoticeList(){
		this._HTMLDom = null;
		this.listData=null;
		this.page=0;
		this._init();	
	}
		
	clsNoticeList.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content article.list01");
			this._HTMLDom = {
				"$layer": $layer	
				, "$element": $layer.find("table tbody tr:first")
				, "$empty": $("article.board_list_none").html("등록된 글이 없습니다.").hide()
			}
			$layer.find("table tbody").empty();
		}
		, setList: function(thisData, objPage){
			this.listData = thisData;			
			this.page = objPage;
			this.display();	
		}
		, display: function(){
			var thisClass = this;
			var $layer = this._HTMLDom["$layer"].find("table tbody");
			var $element = this._HTMLDom["$element"];
			
			
			$layer.empty();
			
			//this.isNoData();
			
			for(var i=0;i<this.listData.length;++i){
				$layer.append($element.clone(true));
			}
			
			var num = this.getPagePerNum(); 
				
			$layer.find("tr").each(function(n){
				$(this).find("td:eq(0)").html(num--);
				$(this).find("td:eq(1)").html(thisClass.listData[n]["구분명"]);
				$(this).find("td:eq(2)").html(thisClass.listData[n]["제목"]);
				$(this).find("td:eq(3)").html(document.objControl.getFormatDateYmd(thisClass.listData[n]["등록일자"],"-"));
				$(this).find("td:eq(4)").html(document.objControl.getFormatNumber(thisClass.listData[n]["조회수"]));
			});
		}
		, spinStart: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("table").hide();
			if($layer.find("#tmpSpin").length == 0){
				$layer.append("<div id='tmpSpin' style='width:650px; height:300px;'></div>");
			}
			$layer.find("#tmpSpin").spin().show();
		}
		, spinEnd: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("#tmpSpin").spin(false).hide();
			$layer.find("table").show();
		}
		, $getTitle: function(){		
			return this._HTMLDom["$layer"].find("table tbody tr td.align_l");
		}
		, isNoData: function(){
			if(!this.listData.length) {
				this._HTMLDom["$empty"].show();
				$("div#idNoticeListPage").hide();
			}else {
				this._HTMLDom["$empty"].hide();
				$("div#idNoticeListPage").show();
			}	
		}
		, getPagePerNum: function(){
			var $page = this.page;
			return $page.getTotalCount() - ($page.getCountPerPage()*($page.getPage()-1));
		}
	}

	
	function clsComm()
	{
		this.noticeList = null;
		this.noticeListRequest = null;
		this.noticeListPageData = null;
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){		
			
			this.noticeListRequest = {
				"페이지": null
			};
			
			this.noticeListPageData = {
				"전체수": null
			};
		}
//------------------------ 공지사항목록------------------------//	
		, getNoticeList: function(){
			var thisClass = this;
			
			var szPage = this.noticeListRequest["페이지"];
			var szServicename = "getNoticeList";
			
			// ajax 시작
			_gf.ajax({
				"sid":"MB001",
				"servicename": szServicename,
				"listcount":10,
				"currpageno":szPage
			}, {
								
			}, function(ret){
				// callback and mapping
				thisClass.noticeList = [];
				
				thisClass.noticeListPageData = {
					"전체수": ret["msp"]["contents"]["@totalcount"]
				}
					
				if(ret["msp"]["contents"]["items"]){					
					if(!ret["msp"]["contents"]["items"].length){
						ret["msp"]["contents"]["items"]	= new Array(ret["msp"]["contents"]["items"]);
					}					
					for(var i=0;i<ret["msp"]["contents"]["items"].length;i++){													
						var obj = ret["msp"]["contents"]["items"][i];
												
						thisClass.noticeList.push({
							"게시판키": _util.nvl(obj["board__seq"])
							, "구분명": _util.nvl(obj["notice__type__name"])
							, "제목": _util.nvl(obj["title"])
							, "등록일자": _util.nvl(obj["reg__date"])
							, "조회수": _util.nvl(obj["hit"])
						});
					}
				}
				
				$(thisClass).trigger("evtFinishNoticeList" );
			});
		}
		, getNoticeListData: function(){
			return this.noticeList;
		}
		, getNoticeListPageData: function(){
			return this.noticeListPageData;
		}
		, setNoticeListRequest: function(key, val){
			this.noticeListRequest[key] = val;
		}
//------------------------ 공지사항목록------------------------//			
	}
	
	function clsControl()
	{
		this.objComm = null;
		
		this.objNoticeList = null;
	
	}
	
	clsControl.prototype = {
		_init: function(){
			
			this.notice_key = _util.getParam("notice_key");;
			
			this.objComm = new clsComm();
			this.objNoticeList = new clsNoticeList();
			this.objNoticeListPage = new _gf.makePaging($("#idNoticeListPage"), 10, 10);		
			
			this._bind();
			
			//공지사항조회 
			//var page = (_util.getParam("page")?_util.getParam("page"):1);
			var page = 1;
			this.objNoticeList.spinStart();		
			this.objNoticeListPage.setPage(page);
			this.objComm.setNoticeListRequest("페이지",page);
			this.objComm.getNoticeList();
		}
		, _bind: function(){
			var thisClass = this;
			
			//공지사항조회 완료 이벤트
			$(this.objComm).bind("evtFinishNoticeList", function(){ 
				var thisData = thisClass.objComm.getNoticeListData();	
				var pageData = thisClass.objComm.getNoticeListPageData();
				
				thisClass.objNoticeList.spinEnd();
				//alert(thisData.length);
				// 페이지 부에 전체 수설정 				
				thisClass.objNoticeListPage.setTotalCount(pageData["전체수"]);
				thisClass.objNoticeListPage.display();
				thisClass.objNoticeList.setList(thisData, thisClass.objNoticeListPage);
			});
			
			// 페이지이동 이벤트
			$(this.objNoticeListPage).bind("evtPageClick", function(){
				var thisPage = this.getPage();
				
				thisClass.objNoticeList.spinStart();
				thisClass.objComm.setNoticeListRequest("페이지", thisPage);				
				thisClass.objComm.getNoticeList();
				
				
			});
			
			//제목 클릭 이벤트(상세보기)
			this.objNoticeList.$getTitle().live("click",function(event){			
				event.preventDefault();	
				var idx = thisClass.objNoticeList.$getTitle().index($(this));
				document.location.href = "notice_view.html" + thisClass.getParam(thisClass.objNoticeList.listData[idx]["게시판키"]);
			});
			
		}
		, getParam: function(n){
			return "?notice_seq="+n +"&page="+this.objNoticeListPage.getPage();
		}
		, getFormatDateYmd: function(n,v){			
			if(n=="") return n;
			n = String(n);
			
			return n.substr(0,4) +v+ n.substr(4,2) +v+ n.substr(6,2);
		}
		, getFormatTime: function(n,v){		
			if(n=="") return n;
			n = String(n);			
			return n.substr(0,2) +v+ n.substr(2,2);
		}
		, getFormatNumber: function(n){
			if(n=="") return n;
			n = String(n);			
			i = n.length;
			r = "";
			while(true){				
				if(i<=3) {
					r = n.substr(0,i) + r;					
					break;
				}
				r = ","+ n.substr(i-3,3) + r;				
				i-=3;
			}
			return r;
		}	
	}
	
	document.objControl = new clsControl();
	document.objControl._init();
});	